import re

import pymongo

from database.db import movieCol
from downloader import download


def parse_director(desc):
    try:
        director = re.findall('导演[|:]?(.*?)<', desc)[0]
        return director
    except:
        return ''


def parse_actor(desc):
    actors = re.findall('[(主演)(演员)][|:]?(.*?)<', desc)[0].split('/')
    return actors


def parse_language(desc):
    try:
        language = re.findall('语言[|:]?(.*?)<', desc)[0].split('/')
        return language
    except:
        return ''


def parse_released_date(desc):
    date = re.findall(r'(\d{4}-\d{2}-\d{2})', desc)[0]
    return date


def parse_categories(desc):
    categories = re.findall('类[型别][|:]?(.*?)<', desc)[0].split('/')
    return categories


def parse_imdb(desc):
    try:
        score = re.findall('IMDb评分[|:]?(.*/10)', desc)[0]
        return score
    except:
        return ''


def parse_douban(desc):
    try:
        score = re.findall('豆瓣评分[|:]?(.*/10)', desc)[0]
        return score
    except:
        return ''


def parse_intro(desc):
    texts = [x for x in desc.split('\n') if x.strip()]
    return texts[-1].strip()


def parse_countries(description):
    return re.findall('[(国家/地区}|(产地)][|:]?(.*?)<', description)[0].split('/')


def parse_name(resp):
    title = resp.xpath('//h1/text()').extract_first()
    return re.findall('《(.*)》', title)[0]


def extract_date(url):
    return re.findall(r'(\d{4}-\d{2}-\d{2})', url)[0]


if __name__ == '__main__':
    while movie := movieCol.find_one({'$and': [
        {'online': {'$exists': True}},
        {'online': {'$ne': []}},
        {'name': {'$exists': False}}
    ]}, sort=[("date", pymongo.DESCENDING)]):
        url = movie['url']
        print(url, movie['title'])
        resp = download(url)
        name = parse_name(resp)
        description = resp.xpath('//meta[@name="description"]/@content').extract_first()
        description = description.replace('\u3000', '')
        director = parse_director(description)
        actors = parse_actor(description)
        try:
            release_date = parse_released_date(description)
        except:
            release_date = extract_date(url)
        language = parse_language(description)
        introduction = parse_intro(description)
        imdb = parse_imdb(description)
        douban = parse_douban(description)
        countries = parse_countries(description)
        try:
            categoreis = parse_categories(description)
        except:
            categoreis = []
        detail = {
            'name': name,
            'director': director,
            'actors': actors,
            'release_date': release_date,
            'language': language,
            'introduction': introduction,
            'imdb': imdb,
            'douban': douban,
            'category': categoreis
        }

        movieCol.update_one({'_id': movie['_id']},
                            {'$set': detail})
